package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/list")
    @RequiresPermissions("permission:list")
    public String list(Model model,QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        System.out.println(pageInfo.toString());
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions("permission:delete")
    public String delete(Long id) {
        if (id != null && !id.equals("")) {
            permissionService.delete(id);
        }
        return "redirect:/permission/list";  //返回的是上面查询所偶有所的方法
    }

//    @RequestMapping("/input")
//    public String input(Long id, Model model) {
//        if (id != null && !id.equals("")) {
//            Permission permission = permissionService.get(id);
//            model.addAttribute("permission", permission);
//        }
//        return "permission/input";
//
//    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions("permission:saveOrUpdate")
    public String saveOrUpdate(Permission permission) {

        Long id = permission.getId();
        if (id != null) {
            permissionService.update(permission);
        }else {
            permissionService.save(permission);
        }
        return "redirect:/permission/list";
    }

    //加载权限
    @RequestMapping("/reload")
    @RequiresPermissions("permission:reload")
    public String reload() {
    //调用加载权限方法
        permissionService.reload();
        return "redirect:/permission/list";
    }

    //跳转到没有权限界面
    @RequestMapping("/nopermission")
    public String nopermission() {
        return
        "common/nopermission.html";
    }
}
